﻿using Wholesaler.Models.DBModels;
using System.Collections;
using Yunzuan.Foundation.DataAccess;
using Yunzuan.Foundation.Web;
using System.Configuration;
using Yunzuan.Common.Log;
using YunZuan.Models;

namespace Yunzuan.Common
{
    public class GoodsType
    {
        public const int Diamond = 1; //裸钻
        public const int Parcel = 2;//统包
        public const int Ring = 3;//成品
        public const int Gem = 4;//彩宝
        public const int Emerald = 5;//翡翠 
        public const int PrimeGolds = 6;//素金  
        public const int InlaySettings = 7;//空托
        public const int Other = 8;//其他 
         
         
        public static string products = ConfigurationManager.AppSettings["AllProducts"];
        //public static string[] GoodName = products.Split('|');//        new string[] { "", "stocks", "parcels", "inlays", "gems", "emeralds", "prime-golds", "inlay-settings" };

        /// <summary>
        /// 得到商品接口
        /// </summary>
        /// <param name="filters"></param>
        /// <param name="goodsType"></param>
        /// <returns></returns>
        public static string GetGoodsUrlByTypId(string filters, int goodsType = 1)
        {
            string url = "";
            switch (goodsType)
            {
                case Diamond:
                    url =  string.Format(GlobalConfig.TransmissionStockOprateUrl, filters);
                    break;
                case Parcel:
                    url =  string.Format(GlobalConfig.TransmissionParcelOprateUrl, filters);
                    break;
                case Ring:
                    url =  string.Format(GlobalConfig.TransmissionInlaysOprateUrl, filters);
                    break;
                case Gem:
                    url =  string.Format(GlobalConfig.TransmissionGemOprateUrl, filters);
                    break;
                case Emerald:
                    url = string.Format(GlobalConfig.TransmissionEmeraldOprateUrl, filters);
                    break;
                case PrimeGolds:
                    url = string.Format(GlobalConfig.TransmissionPrimeGoldsOprateUrl, filters);
                    break;
                case InlaySettings:
                    url =  string.Format(GlobalConfig.TransmissionInlaySettingsOprateUrl, filters);
                    break;
            }
            return url;
        }

        /// <summary>
        /// 得到商品接口名
        /// </summary>
        /// <param name="filters"></param>
        /// <param name="goodsType"></param>
        /// <returns></returns>
        public static string GetGoodsName(int goodsType = 1)
        {
             string[] GoodName = products.Split('|');
            //索引超出
            if (goodsType >= GoodName.Length)
            {
                CommonLog.SendMQMsg("GetGoodsName", $"goodsType:{goodsType} , GoodName:{GoodName}");
                return GoodName[0];
            }
            else
                return GoodName[goodsType];
        }

        public static SysSettingInfo GetSystemSetting(string tttagKey, int tType)
        {
            SysSettingInfo setting = new SysSettingInfo();
            Hashtable hash = new Hashtable(); 
            hash.Add("SysSettingID", tType);
            setting = YZ.GetDatabase(tttagKey).QueryForObject<SysSettingInfo>("Syssetting.GetSetting", hash);
            return setting;
        }
        public static dynamic GetSystemSetting<T>(string tttagKey, int tType)
        { 
            Hashtable hash = new Hashtable(); 
            hash.Add("SysSettingID", tType);
            var setting = YZ.GetDatabase(tttagKey).QueryForObject<T>("Syssetting.GetSetting", hash);
            return setting;
        }
     

        public static string GetTypeName(int tType)
        {
            string typeName = "";
            switch (tType)
            {
                case GoodsType.Diamond:  //库存 
                    typeName = "裸钻";
                    break;
                case GoodsType.Parcel: //统包
                    typeName = "统包";
                    break;
                case GoodsType.Ring:   //成品
                    typeName = "成品";
                    break;
                case GoodsType.Gem:    //彩宝
                    typeName = "彩宝";
                    break;
                case GoodsType.Emerald:    //翡翠
                    typeName = "翡翠";
                    break;
                case GoodsType.PrimeGolds:    //素金
                    typeName = "素金";
                    break;
                case GoodsType.InlaySettings:    //空托
                    typeName = "空托";
                    break;
            }
            return typeName;
        }
        
        /// <summary>
        /// 全局销售汇率 -裸钻
        /// </summary>
        /// <returns></returns>
        public static decimal GetRate(SysSettingInfo setting)
        {
            if (setting.Rate.HasValue && setting.Rate > 0)
            {
                return setting.Rate.Value;
            }
            return 1;
        }


        /// <summary>
        /// 全局采购汇率 - 裸钻
        /// </summary>
        /// <returns></returns>
        public static decimal GetPurchaseExchangeRate(SysSettingInfo setting)
        {
            if (setting.PurchaseExchangerate.HasValue && setting.PurchaseExchangerate > 0)
            {
                return setting.PurchaseExchangerate.Value;
            }
            return 1;
        }


        /// <summary>
        /// 全局白钻退点
        /// </summary>
        /// <returns></returns>
        public static decimal GetGlobalDiscountReturn(SysSettingInfo setting)
        {
            if (setting.GlobalDiscountReturn.HasValue)
            {
                return setting.GlobalDiscountReturn.Value;
            }
            return 0;
        }

        /// <summary>
        /// 全局彩钻退点
        /// </summary>
        /// <returns></returns>
        public static decimal GetGlobalFancyDiscountReturn(SysSettingInfo setting)
        {
            if (setting.GlobalFancyDiscountReturn.HasValue)
            {
                return setting.GlobalFancyDiscountReturn.Value;
            }
            return 0;
        }

        
        /// <summary>
        /// 得到商品接口
        /// </summary>
        /// <param name="filters"></param>
        /// <param name="goodsType"></param>
        /// <returns></returns>
        public static string GetGoodsURL(int goodsType = 1)
        {
            return GetGoodsUrlByTypId(GlobalConfig.filters, goodsType);
        }

        public static string GetProductExportURL(int goodsType = 1)
        {
            return GetGoodsUrlByTypId(GlobalConfig.ExportPort, goodsType); 
        }

        public static string GetGoodsFilters(int systemType, ref int goodsType)
        {
            string filters = "";
            switch (systemType)
            {

                case GoodsType.Diamond:  //库存 
                    filters = GlobalConfig.IUrl;
                    goodsType = 1;
                    break;
                case GoodsType.Parcel: //统包
                    filters = GlobalConfig.ParcelsUrl ;
                    goodsType = 2;
                    break;
                case GoodsType.Ring:   //成品
                    filters = GlobalConfig.TransmissionInlaysSearchUrl;
                    goodsType = 3;
                    break;
                case GoodsType.Gem:    //彩宝
                    filters = GlobalConfig.TransmissionGemSearchUrl;
                    goodsType = 4;
                    break;
                case GoodsType.Emerald:    //翡翠
                    filters = GlobalConfig.TransmissionEmeraldSearchUrl;
                    goodsType = 5;
                    break;
                case GoodsType.PrimeGolds:    //素金
                    filters = GlobalConfig.TransmissionPrimeGoldsSearchUrl;
                    goodsType = 6;
                    break;
                case GoodsType.InlaySettings:    //素金
                    filters = GlobalConfig.TransmissionInlaySettingsSearchUrl;
                    goodsType = 7;
                    break;
            }
            return filters;
        }
    }
}
